package com.toocms.jinjuhang.ui.mine.order

import android.app.Application
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableField
import com.toocms.tab.base.BaseModel
import com.toocms.tab.base.BaseViewModel
import com.toocms.tab.binding.ItemBinding
import com.toocms.jinjuhang.R
import com.toocms.jinjuhang.BR
import com.toocms.jinjuhang.config.Constants
import com.toocms.jinjuhang.config.Urls
import com.toocms.jinjuhang.config.UserRepository
import com.toocms.jinjuhang.data.order_info.OrderListData
import com.toocms.jinjuhang.widget.view_model.requestData
import com.toocms.tab.binding.command.BindingAction
import com.toocms.tab.binding.command.BindingCommand
import com.toocms.tab.bus.Messenger
import com.toocms.tab.bus.event.SingleLiveEvent

class MyOrderModel(application: Application) : BaseViewModel<BaseModel>(application) {

    var p = 1

    var currentOrderStatus = OrderStatus.ALL
        set(value) {
            field = value
            refresh()
        }

    val items = ObservableArrayList<MyOrderItemModel>()
    val itemBinding = ItemBinding.of<MyOrderItemModel>(BR.myOrderItemModel, R.layout.item_order)

    val stopRefreshOrLoadSingleLiveEvent = SingleLiveEvent<Unit>()

    val refreshBindingCommand = BindingCommand<BindingAction>(BindingAction {
        refresh()
    })

    val loadBindingCommand = BindingCommand<BindingAction>(BindingAction {
        load()
    })

    init {
        initializeMessenger()
        refresh()
    }

    private fun initializeMessenger(){
        Messenger.getDefault().register(this,Constants.MessengerToken.TOKEN_REFRESH_ORDER){
            refresh()
        }
    }

    private fun refresh(isShowLoading: Boolean = false) {
        p = 1
        requestData(currentOrderStatus,p, isShowLoading)
    }

    private fun load() {
        p++
        requestData(currentOrderStatus,p)
    }

    private fun requestData(status:OrderStatus,p: Int, isShowLoading: Boolean = false) {
        val url: String
        val params = mutableMapOf<String, String?>()
        params["member_id"] = UserRepository.user.member_id
        params["p"] = p.toString()
        when (status) {
            OrderStatus.AFTER_SALE -> {
                //TODO 退款列表
                url = Urls.OrderInfo.REFUND_LIST
            }
            else -> {
                //TODO 订单列表
                url = Urls.OrderInfo.ORDER_LIST
                params["status"] = currentOrderStatus.code.toString()
            }
        }
        requestData(url,
            params,
            resultClass = OrderListData::class.java,
            isShowLoading = isShowLoading,
            result = {
                if(status!=currentOrderStatus)return@requestData
                if (1 == p) items.clear()
                it.list?.forEach {
                    items += MyOrderItemModel(this, ObservableField(it))
                }
            },
            finish = {
                stopRefreshOrLoadSingleLiveEvent.call()
            })
    }


}